package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: 李旭
 * @create: 2021-08-24 11:11
 *
 *   接收请求
 *
 *   后台管理
 *     --- 商品管理

 **/
@RestController
@RequestMapping("/admin/product")
//@CrossOrigin //Cross 跨   Origin域
public class ManageController {

    // VUE 获取一级分类   http://localhost:8206/admin/product/getCategory1

    @Autowired
    private ManageService manageService;

    //获取一级分类集合 无条件
    @GetMapping("/getCategory1")
    public Result getCategory1(){

        List<BaseCategory1> baseCategory1List = manageService.getCategory1();

        return Result.ok(baseCategory1List);//code:200 message:成功
    }
    //获取二级分类
    @GetMapping("/getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable Long category1Id){
        List<BaseCategory2> baseCategory2List = manageService.getCategory2(category1Id);
        return Result.ok(baseCategory2List);
    }
    //获取三级分类
    @GetMapping("/getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable Long category2Id){
        List<BaseCategory3> baseCategory3List = manageService.getCategory3(category2Id);
        return Result.ok(baseCategory3List);
    }
    //查询平台属性及属性值集合
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result  attrInfoList(
            @PathVariable Long category1Id,
            @PathVariable Long category2Id,
            @PathVariable Long category3Id
    ){
        List<BaseAttrInfo> baseAttrInfoList = manageService.attrInfoList(category1Id,
                category2Id,category3Id);
        return Result.ok(baseAttrInfoList);
    }

    //添加平台属性   约定：遵守RestFul风格开发 添加 POST 修改PUT  删除DELETE  查询GET
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){

        //保存
        manageService.saveAttrInfo(baseAttrInfo);

        return Result.ok();
    }
    //品牌列表查询 （分页） 每页数  当前页
    @GetMapping("/baseTrademark/{page}/{limit}")
    public Result baseTrademark(@PathVariable Integer page,
                                 @PathVariable Integer limit){

        //Mybatis-Plus 框架 自带 分页对象
        IPage iPage = manageService.baseTrademark(page,limit);
        return Result.ok(iPage);
    }
    //平台属性 去修改页面  根据ID查询平台属性
    @GetMapping("/getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable Long attrId){
        List<BaseAttrValue> baseAttrValueList = manageService.getAttrValueList(attrId);
        return Result.ok(baseAttrValueList);
    }
    //查询商品列表  分页  根据三级分类ID进行查询
    @GetMapping("/{page}/{limit}")
    public Result spuPage(@PathVariable Integer page,@PathVariable Integer limit,
                           Long category3Id){
        IPage iPage = manageService.spuPage(page,limit,category3Id);
        return Result.ok(iPage);
    }

    //品牌集合  商品添加 显示下拉框
    @GetMapping("/baseTrademark/getTrademarkList")
    public Result getTrademarkList(){
        List<BaseTrademark> trademarkList = manageService.getTrademarkList();
        return Result.ok(trademarkList);
    }

    //销售属性集合  商品添加  显示下拉框
    @GetMapping("/baseSaleAttrList")
    public Result baseSaleAttrList(){
        List<BaseSaleAttr> baseSaleAttrList = manageService.baseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    //商品添加
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        //保存
        manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    //根据spuId查询图片列表
    @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable Long spuId){
        List<SpuImage> spuImageList = manageService.spuImageList(spuId);
        return Result.ok(spuImageList);
    }

    //根据spuId查询销售属性及属性值集合
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable Long spuId){

        List<SpuSaleAttr>  spuSaleAttrList = manageService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }
    //添加sku
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        manageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    //查询SKU分页列表
    @GetMapping("/list/{page}/{limit}")
    public Result skuListPage(@PathVariable Integer page,@PathVariable Integer limit){
        IPage iPage =  manageService.skuListPage(page,limit);
        return Result.ok(iPage);
    }

    //上架
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        manageService.onSale(skuId);
        return Result.ok();
    }
    //下架
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        manageService.cancelSale(skuId);
        return Result.ok();
    }



}
